package code.temp;

import java.util.concurrent.CountDownLatch;
import java.util.concurrent.atomic.AtomicInteger;

/**
 * <b>Description : </b>
 *
 * @author CPF
 * @date 2021/5/20 11:25
 **/
public class NumberPrint2 {


    public static int i = 1;
    public static Object lock = new Object();
    private static AtomicInteger num = new AtomicInteger(1);
    private static CountDownLatch countDownLatch = new CountDownLatch(2);
    public static void main(String[] args) throws InterruptedException {
        doCountDownLatch();
    }
    /**
     * countdownlatch 实现
     */
    private static void  doCountDownLatch(){
        Thread t1 = new Thread() {
            @Override public void run() {
                while (num.intValue() < 100) {
                    if (num.intValue() % 2 == 1) {

                        System.out.println("奇数线程:" + num.intValue());
                        num.incrementAndGet();
                    }
                    countDownLatch.countDown();
                }
            }
        };

        Thread t2 = new Thread() {
            @Override public void run() {
                while (num.intValue() <= 100) {
                    if (num.intValue() % 2 == 0) {
                        System.out.println("偶数线程:" + num.intValue());
                        num.incrementAndGet();
                    }
                    countDownLatch.countDown();
                }
            }
        };
        t1.start();
        t2.start();
        try {
            countDownLatch.await();
            System.out.printf("dss");
            countDownLatch.await();
            System.out.printf("dss");
            countDownLatch.await();
            System.out.printf("dss");
            countDownLatch.await();
            System.out.printf("dss");
            countDownLatch.await();
            System.out.printf("dss");
            countDownLatch.await();
            System.out.printf("dss");
            countDownLatch.await();
            System.out.printf("dss");
        }catch (Throwable e){
            System.out.println(e);
        }
    }

}
